/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pubguru.data;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;

/**
 *
 * @author Dao
 */
public class PaperData {
    private static PaperData _instance;
    public static PaperData getInstance()
    {
        if (_instance == null)
            _instance = new PaperData();
        return _instance;
    }
    
    private PaperData()
    {
        paperData = new HashMap<>();
        mappingId = new HashMap<>();
        yearData = new HashMap<>();
        
        numberOfOverlappedPaper = 0;
        currIdPaper = 0;
    }
    
    private HashMap<Integer, String> paperData;  //key : hashcode of value , value : standard paper title
    
    private HashMap<Integer, Integer> mappingId;  //key : hashcode of standard paper title , value : paper id
    
    private HashMap<Integer, Integer> yearData;  //key : hashcode of standard paper title , value : year
    
    private int currIdPaper;
    
    private int numberOfOverlappedPaper;
    
    //test
    public int totalPaperMissingInfor = 0;
    
    public void LoadData(String fileName) throws FileNotFoundException, IOException
    {
        FileReader fr = new FileReader(fileName);
        BufferedReader br = new BufferedReader(fr);
        br.readLine(); //ignore first line
        String str="";
        String[] tokens;
        String paperName = "";
        String standardPaperName = "";
        int paperId = 0;
        int paperYear = 0;
        int count = 0;
        int count2 = 0;
        int hashCode = 0;
        while ((str=br.readLine()) != null)
        {
            count++;
            tokens = str.split("\t");
            if (tokens.length >= 2)
            {
                paperId = Integer.parseInt(tokens[0]);
                paperName = tokens[1];
                if (tokens.length == 3)
                {
                    try
                    {
                        paperYear = Integer.parseInt(tokens[2]);
                    }catch (Exception e)
                    {
                        paperName += tokens[2];
                        if (tokens.length == 4)
                            paperYear = Integer.parseInt(tokens[3]);
                    }
                }
                else
                    paperYear = 0;
                standardPaperName = paperName.replace(",", "").replace(".","").replace("/","").replace("?","").replace(":", "").replace("!","").replace("\\", "").replace("'", "").replace("-", "").replace(":", "").replace("\\", "").toLowerCase().replaceAll("\\s+", "");
                hashCode = standardPaperName.hashCode();
                paperData.put(hashCode, standardPaperName);
                mappingId.put(hashCode, paperId);
                yearData.put(hashCode, paperYear);
                currIdPaper = paperId;
            }
            else
            {
                count2++;
                //System.out.println("Error at " + count + " " + fileName);
            }
        }
        br.close();
        fr.close();
        System.out.println("Total paper " + fileName + " : " + count);
        System.out.println("Total paper missing information " + fileName + " : " + count2);
        totalPaperMissingInfor = count2;
    }
    
    public void AddPaper(String paperName,int year)
    {
        int hashCode = paperName.hashCode();
        if (!paperData.containsKey(hashCode))
        {
            AddNewPaper(hashCode, paperName,year);
        }     
        else
            numberOfOverlappedPaper++;
    }
        
    private void AddNewPaper(int hashCode,String paperName,int year)
    {
        paperData.put(hashCode, paperName);
        mappingId.put(hashCode, currIdPaper);
        yearData.put(hashCode, year);
    }
    
    public int GetNumberOfPaper()
    {
        return paperData.size();
    }
    
    public int GetNumberOfOverlappedPaper()
    {
        return numberOfOverlappedPaper;
    }
}
